package edu.ou.herbarium.main;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ConveyorAnimation {
	private static final int IMAGE_WIDTH = 30;
	// The timer interval in milliseconds
	private static final int TIMER_INTERVAL = 10;
	private int x = 0;
	private int y = 0;
	// The direction the "bar" is moving
	private int directionX = 1;
	private int directionY = 1;
	private Canvas canvas;

	public void run() {
		final Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Animator Double Buffer");
		createContents(shell);
		shell.open();

		// Set up the timer for the animation
		Runnable runnable = new Runnable() {
			public void run() {
				animate();
				display.timerExec(TIMER_INTERVAL, this);
			}
		};

		display.timerExec(TIMER_INTERVAL, runnable);

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		// Kill the timer
		display.timerExec(-1, runnable);
		display.dispose();
	}

	private void createContents(final Shell shell) {
		shell.setLayout(new FillLayout());

		// Create the canvas for drawing
		canvas = new Canvas(shell, SWT.NO_BACKGROUND);
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
				// Create the image to fill the canvas
				Image image = new Image(shell.getDisplay(), canvas.getBounds());
				// Set up the offscreen gc
				GC gcImage = new GC(image);
				// Draw the background
//				gcImage.setBackground(event.gc.getBackground());
//				gcImage.fillRectangle(image.getBounds());

				// Set the color of the ball
				gcImage.setBackground(shell.getDisplay().getSystemColor(
						SWT.COLOR_BLUE));

				// Draw the ball
				gcImage.fillOval(x, y, IMAGE_WIDTH, IMAGE_WIDTH);
				gcImage.fillOval(x+100, y, IMAGE_WIDTH, IMAGE_WIDTH);
				gcImage.fillRectangle(x+IMAGE_WIDTH/2, y, 100, 2);
				gcImage.fillRectangle(x+IMAGE_WIDTH/2, y+IMAGE_WIDTH, 100, 2);
				

				// Draw the offscreen buffer to the screen
				event.gc.drawImage(image, 0, 0);

				// Clean up
				image.dispose();
				gcImage.dispose();
			}
		});
	}

	public void animate() {
		// Determine the ball's location
		x += directionX;
//		y += directionY;

		// Determine out of bounds
		Rectangle rect = canvas.getClientArea();
		if (x < 0) {
			x = 0;
			directionX = 1;
		} else if ((x+100) > rect.width - IMAGE_WIDTH) {
			x = rect.width - IMAGE_WIDTH-100;
			directionX = -1;
		}
//		if (y < 0) {
//			y = 0;
//			directionY = 1;
//		} else if (y > rect.height - IMAGE_WIDTH) {
//			y = rect.height - IMAGE_WIDTH;
//			directionY = -1;
//		}

		// Force a redraw
		canvas.redraw();
	}

	public static void main(String[] args) {
		new ConveyorAnimation().run();
	}
}
